<?php
namespace Modules\Admin\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Goods;
use App\Models\Order;
use App\Models\OrderGoods;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class DataController extends Controller{
    //商品信息统计
    public function dataGoodsTongjiApi(Request $request){
        try{

            $startDate = $request->input("start_time",false);
            $endDate = $request->input("end_time",false);

            //查询商品
            $goods = Goods::query()->get();

            //查询该时间段内商品销量
            $query = OrderGoods::query();
            if ($startDate){
                $query->whereDate("created_at",">=",$startDate);
            }
            if ($endDate){
                $query->whereDate("created_at","<=",$endDate);
            }

            $goodsNew = [];
            if ($goods->count()>0){
                foreach ($goods as $item){
                    $orderGoodsData = $query->where('goods_id',$item->id)->with('order')->get();
                    $sellNum = 0;
                    if ($orderGoodsData->count()>0){
                        foreach ($orderGoodsData as $value){
                            if ($value->order->order_status>0){
                                $sellNum+=$value->goods_num;
                            }
                        }
                    }
                    $goodsNew[] = ["id"=>$item->id,"goods_name"=>$item->goods_name,"sell_num"=>$sellNum];
                }
            }
            return self::jsondata(0,$goodsNew);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //订单信息统计
    public function dataOrderTongjiApi(){
        try{

        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }


}